package com.rq.rvlibrary;

import android.view.View;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class OnClickMaker {
  List<OnItemClickListener> clickListeners = new ArrayList<>();
  int[] clickIds;

  public OnClickMaker(OnItemClickListener clickListener, int... ids) {
    this.clickListeners.add(clickListener);
    this.clickIds = ids;
  }

  public void addClickListener(OnItemClickListener clickListener) {
    clickListeners.add(clickListener);
  }

  public boolean noClick() {
    return clickListeners.size() == 0;
  }

  public <DATA> void onItemClick(BaseAdapter parentAdapter, BaseViewHolder<DATA> dataBaseViewHolder,
                                 Object data, View v, int mPosition) {
    if (clickListeners.size() > 0) {
      for (OnItemClickListener clickListener : clickListeners) {
        clickListener.onItemClick(parentAdapter, dataBaseViewHolder, data, v, mPosition);
      }
    }
  }

  public void addIds(int[] ids) {
    int[] old = OnClickMaker.this.clickIds;
    if (old != null && old.length != 0 && ids != null && ids.length != 0) {
      this.clickIds = concat(old, ids);
    } else if (old == null || old.length == 0) {
      this.clickIds = ids;
    }
  }

  //合并数组
  public static int[] concat(int[] first, int[] second) {
    int[] result = Arrays.copyOf(first, first.length + second.length);
    System.arraycopy(second, 0, result, first.length, second.length);
    return result;
  }
  //  //数组去重
  // 　　//第一种：使用set集合无序不重复
  //
  //  public static void setDistinct(int[] arr) {
  //    HashSet<Integer> set = new HashSet<Integer>();
  //    for (Integer i : arr) {
  //      set.add(i);
  //    }
  //    Integer[] newArr = set.toArray(new Integer[1]);
  //    System.out.println(Arrays.toString(newArr));
  //  }
}
